let axios = require('axios')
const { access_token } = require('../config/config')
let request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=" + access_token;

// 百度动物识别API 获取用户的access_token
const getNewAccess_Token = async () => {
  // const addr = encodeURI(address)
  const res = await axios({
    method: 'get',
    url: "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=DhMxRYGGsTTTYBDvWGnWLXe3&client_secret=aWGXqnxWBIwWnOfQOmYR6I5i8rYILPEG&",
    headers: { 'Content-Type': 'application/json' }
  })
  return res.data.access_token
}

const getImgIdentyByUrl = async (imgURL) => {
  // const addr = encodeURI(address)
  let url = request_url
  const res = await axios({
    method: 'post',
    url: request_url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'application/json'
    },
    params: {
      "url": imgURL
    }
  })
  return res
}

const getImgIdentyByBase64 = async (encode) => {
  let url = request_url
  const res = await axios({
    method: 'post',
    url: request_url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'application/json'
    },
    params: {
      "image": encode
    }
  })
  return res
}

module.exports = {
  getNewAccess_Token,
  getImgIdentyByUrl,
  getImgIdentyByBase64
}